/**
 * @Author: SevDaisy十七散人 
 * @Date: 2020-07-04 09:22:29
 */
package zyy.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import zyy.util.BaseException;
import zyy.util.Bus;;

public class FrmLogin extends JDialog implements ActionListener {

  /**
   * Login 帧变量
   */
  private static final long serialVersionUID = -2830922578176219577L;
  // private String LoginAccountType = Link.LoginAccountType;

  // ui 两个Div
  private JPanel toolBar = new JPanel();
  private JPanel workPane = new JPanel();

  // ui 按钮
  private JButton btnLogin = new JButton("登陆");
  private JButton btnCancel = new JButton("退出");
  private JButton btnRegister = new JButton("注册");

  // ui 账户
  private JLabel labelUser = new JLabel("账户：");
  private JTextField edtLoginName = new JTextField(20);
  // private GroupLayout gUser = new GroupLayout(workPane);

  // ui 密码
  private JLabel labelPwd = new JLabel("密码：");
  private JPasswordField edtPwd = new JPasswordField(20);
  // private GroupLayout gPwd = new GroupLayout(workPane);

  // ui 单选项
  private JPanel jrPane = new JPanel();
  private JRadioButton jrUser = new JRadioButton("用户");
  private JRadioButton jrRider = new JRadioButton("骑手");
  private JRadioButton jrRoot = new JRadioButton("管理员", true);
  private ButtonGroup btnG = new ButtonGroup();// btnG.add(jrUser); btnG.add(jrRoot); // 绑定n个单选为n选一

  public FrmLogin(Frame f, String s, boolean b) {
    super(f, s, b);
    {

      // {
      // // 测试 JDialog 有无菜单栏
      // JMenuBar jmb = new JMenuBar();
      // JMenu menu_Choose = new JMenu("选择界面");
      // JMenuItem menuItem_Rider = new JMenuItem("骑手");
      // this.setJMenuBar(jmb);
      // jmb.add(menu_Choose);
      // menu_Choose.add(menuItem_Rider);
      // // 测试结果： JDialog Support JMenuBar == true
      // }

      {
        // btn -> 按钮区Panel 流式布局: flow right
        toolBar.setLayout(new FlowLayout(FlowLayout.RIGHT));
        toolBar.add(this.btnRegister);
        toolBar.add(this.btnLogin);
        toolBar.add(this.btnCancel);
        // 按钮区Panel:toolBar -> bodyPanel 边框布局: SOUTH
        this.getContentPane().add(toolBar, BorderLayout.SOUTH);
      }
      {
        // Label+Field -> workPanel
        // workPane.setLayout(new FlowLayout(FlowLayout.LEFT));
        workPane.add(labelUser);
        workPane.add(edtLoginName);
        workPane.add(labelPwd);
        workPane.add(edtPwd);
        // System.out.println("workPane is : " + workPane.getLayout());
        this.getContentPane().add(workPane, BorderLayout.CENTER);
      }
      {
        // 调试预设账密
        this.edtLoginName.setText("1");
        this.edtPwd.setText("1");
        this.btnLogin.setSelected(true);
      }
      {
        // 单选框 中 加入
        jrPane.setLayout(new FlowLayout(FlowLayout.CENTER));
        jrPane.add(jrUser);
        jrPane.add(jrRider);
        jrPane.add(jrRoot);
        // 绑定 n 个单选按钮为同一个 ButtonGroup 则 n个单选项中同时最多只有一个会被选中
        btnG.add(jrUser);
        btnG.add(jrRider);
        btnG.add(jrRoot);
        this.workPane.add(jrPane, BorderLayout.SOUTH);
      }
      // 设置登陆界面窗口大小
      this.setSize(312, 175);
      {
        // 屏幕居中显示
        double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        this.setLocation((int) (width - this.getWidth()) / 2, (int) (height - this.getHeight()) / 2);
      }
      // 自适应调整组件大小
      this.validate();
    }

    {
      // 事件监听管理
      // 按钮事件
      this.btnLogin.addActionListener(this);
      this.btnCancel.addActionListener(this);
      this.btnRegister.addActionListener(this);
      this.jrUser.addActionListener(this);
      this.jrRider.addActionListener(this);
      this.jrRoot.addActionListener(this);
      // 窗口事件
      this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      });
    }
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.jrUser) {
      Bus.LoginAccountType = "用户";
    } else if (e.getSource() == this.jrRider) {
      System.out.println("更改登陆类型为 骑手");
      Bus.LoginAccountType = "骑手";
    } else if (e.getSource() == this.jrRoot) {
      Bus.LoginAccountType = "管理员";
    } else if (e.getSource() == this.btnLogin) {
      switch (Bus.LoginAccountType) {
        case "用户":
          String loginName = this.edtLoginName.getText();
          String uPwd = new String(this.edtPwd.getPassword());
          try {
            Bus.currentUser = Bus.userManager.login(loginName, uPwd);
            // System.out.println(Link.currentUser);
            JOptionPane.showMessageDialog(null, "登陆成功: 用户", "恭喜", JOptionPane.INFORMATION_MESSAGE);
            this.setVisible(false);
          } catch (BaseException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            return;
          }
          break;
        case "骑手":
          String rName = this.edtLoginName.getText();
          try {
            Bus.currentRider = Bus.riderManager.login(rName);
            JOptionPane.showMessageDialog(null, "登陆成功: 骑手", "恭喜", JOptionPane.INFORMATION_MESSAGE);
            this.setVisible(false);
          } catch (BaseException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            return;
          }
          break;
        case "管理员":
          String aName = this.edtLoginName.getText();
          String aPwd = new String(this.edtPwd.getPassword());
          this.setVisible(false);
          try {
            Bus.currentAdministrator = Bus.adminManager.login(aName, aPwd);
            JOptionPane.showMessageDialog(null, "登陆成功: 管理员", "恭喜", JOptionPane.INFORMATION_MESSAGE);
          } catch (BaseException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            return;
          }
          break;
      }
      // this.setVisible(false);
    } else if (e.getSource() == this.btnRegister) {
      FrmUserReg dlg = new FrmUserReg(this, "注册", true, Bus.LoginAccountType);
      dlg.setVisible(true);
    } else if (e.getSource() == this.btnCancel) {
      // System.exit(0);
      this.setVisible(false);
    }
  }

  public static void main(String[] args) {
    FrmLogin dlg = new FrmLogin(null, "测试Login", true);
    dlg.setVisible(true);
  }
}